/*
 *  User interface for Chatroom Program
 *  Initially connects to ServerParent1 who starts a ServerChild Thread that talks with this Window
 *  
 *  Run by running Main.java
 *  
 *  future enhancements:  separate user interface and client class
 *  						add message class which is passed back and fother between server and client
 */

package chatroom18;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Window implements ActionListener,WindowListener {

	Frame myFrame;
	JTextArea chatbox;
	JTextField txtName, txtMessage;
	JButton btnName, btnSend;
	String userName;
        Client client;

	
	/**
	 * @param args
	 */
	
	public Window() 
	{
		myFrame = new Frame("Chatroom");
		userName = "Name";
                //client = new Client(this);
	}
	
	/*
	 * Basically a main function
	 * Call it to run the Chatroom
	 */
	public void setup() 
	{
		
		buildFrame();
		client.setupSockets();
	}
	
	
	

	public void writeChatMessage(String message)
        {
            chatbox.append(message);
        }

	/*
	 * (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 * This method is called whenever the button's are pressed
	 */
	public void actionPerformed(ActionEvent arg0) {
		String tmp = arg0.getActionCommand();
		//tmp is the name displayed on the button pressed
		if (tmp.equals("Name"))
		{
			changeName();
		}
		else
		{
                    if (!txtMessage.getText().equals(""))
                    {
			client.sendMessage(userName + ": " + txtMessage.getText());
                        txtMessage.setText("");
                    }
		}
	}
	
	/*
	 * Changes the user's name
	 */
	public void changeName()
	{
		userName = txtName.getText();
		txtName.setText("");
		chatbox.append("Changed name to " + userName + "\n");
	}
	
	

	
	/*
	 *Creates all the buttons, labels, and text boxes and puts them into panel which are then put into the fram 
	 */
	@SuppressWarnings("deprecation")
	public void buildFrame()
	{
		myFrame.setSize(500, 500);
		myFrame.addWindowListener(this);
		
		chatbox = new JTextArea(10, 4);
		chatbox.setWrapStyleWord(true);
		chatbox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		myFrame.add(chatbox, BorderLayout.CENTER);
		
		Panel panName = new Panel();
		Panel panMessage = new Panel();
		
		txtName = new JTextField(30);
		panName.add(txtName, BorderLayout.EAST);
		
		btnName = new JButton("Name");
		btnName.addActionListener(this);
		panName.add(btnName, BorderLayout.WEST);
		myFrame.add(panName, BorderLayout.NORTH);
		
		txtMessage = new JTextField(30);
		panMessage.add(txtMessage, BorderLayout.EAST);
		
		btnSend = new JButton("Send");
		
		panMessage.add(btnSend, BorderLayout.WEST);
		
		btnSend.addActionListener(this);
		myFrame.add(panMessage, BorderLayout.SOUTH);
		
		myFrame.show();

		
	}
	
	
	

	/*
	 * (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
	 * 
	 * These are different methods which are called when the window changes state
	 */
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
	}

	//Called when the user hits the X button
	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
		myFrame.dispose();
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

